﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;
using Module;
using cde.cache;
using cde.UserControl;
using System.Xml;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:       PswModify.aspx.cs         
/// Author :         caoych
/// CDT:             2011-12-15         
/// Version:         1.0.0.0                
/// Depiction:       密码修改    
/// </summary>

public partial class main_admin_PswModify : UserControl.CommonPage
{
    #region 变量
    /// <summary>
    /// 修改密码EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 修改密码Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    /// 修改密码Entity名字
    /// </summary>    
    private const string EM_FULL_NAME = WEBConstants.EM_USER_MANAGE;
    #endregion

    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void OnSave(object sender, EventArgs e)
    {
        string pwd = this.txt_PSW_OLD.Value;
        string pwd1 = this.txt_PSW_NEW.Value;
        string pwd2 = this.txt_PSW_CON.Value;
        LoginOprator login = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        string user = login.NAME;
        CDEEntity member = doLogin(user, pwd);
        //检查DB中是否存在此条数据
        if (member == null)
        {
            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.AppendFormat("alert('旧密码错误！');{0}", Environment.NewLine);
            sbr.AppendFormat("</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Success_Back", sbr.ToString());
            this.txt_PSW_OLD.Focus();
        }
        else
        {
            bool isSaveError = false;
            member.set("PSW", pwd1);
            member.Operation = CDEDataOperation.UPDATE;
            List<CDEEntity> listEntity = new List<CDEEntity>();
            listEntity.Add(member);
            //更新DB
            CDEService.UpdateEntities(member.EntityManager, listEntity);

            foreach (CDEEntity entity in listEntity)
            {
                if (entity.Result.Equals(CDEDataOperationResult.ERROR))
                {
                    isSaveError = true;
                    break;
                }
            }
            //成功返回到登陆页面，失败提示错误信息
            if (isSaveError)
            {
                showMessageErrAndOk(Resources.ResourceErrMsg.SAVE_ERR);
                this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
            }
            else
            {
                //系统日志
                //SysLog.AddSysLog(listEntity, this.GetLoginOprator());
                //Response.Write("<script language=javascript>alert('" + "密码修改成功！" + "');</script>");
                //Response.Write("<script language=javascript>window.close();top.location.href='../../login.aspx ';window.opener.parent.location.href='" + @"../../login.aspx" + "';</script>");
                StringBuilder sbr = new StringBuilder();
                sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
                sbr.AppendFormat("alert('密码修改成功！');{0}", Environment.NewLine);
                sbr.AppendFormat("closeWin()");
                sbr.AppendFormat("</script>{0}", Environment.NewLine);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
            }
        }

    }
    /// <summary>
    /// 旧密码检查
    /// </summary>
    /// <param name="user"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    private CDEEntity doLogin(string user, string pwd)
    {
        CDEEntity member = null;
        em = CDEFactory.getEMByFullName(EM_FULL_NAME);
        CDESearchCriteria sc = new CDESearchCriteria(em);
        sc.addCondition("USER_NAME", user);
        sc.addCondition("PSW", pwd);
        List<CDEEntity> list = CDEService.LoadEntities(em, sc);
        if (list.Count != 1)
        {
            return null;
        }
        else if (!user.Equals(list[0].get("USER_NAME").ToString()))
        {
            return null;
        }
        else
        {
            member = list[0];
            return member;
        }

    }
}

